#include "music.h"
#include <QDebug>
#include <QCoreApplication>
#include <QUuid>
#include <QSqlQuery>
#include <QSqlError>

Music::Music()
{
}

Music::Music(const QUrl &url_)
    :isLike(false)
    ,isHistory(false)
    ,url(url_)
{
    //根据url , 构建音乐元数据 , 名称,歌手,专辑,路径等
    //1. 创建媒体播放对象
    QMediaPlayer player;
    //2. 设置媒体源, 依靠setMedia方法解析元数据
    player.setMedia(url_);
    //3. setMedia不会等待歌曲的加载就立即返回
    //需要保证媒体源已经添加完毕
    while(!player.isMetaDataAvailable()){
        QCoreApplication::processEvents();
    }

    //4. 媒体源加载完成 , 获取元数据
    this->name = player.metaData("Title").toString();
    this->singer = player.metaData("Author").toString();
    this->album = player.metaData("AlbumTitle").toString();
    this->songtime = player.metaData("Duration").toLongLong();
    this->name.trimmed();
    this->singer.trimmed();
    this->album.trimmed();

    if(this->name == "") this->name = "未知歌曲";
    if(this->singer == "") this->singer = "未知歌手";
    if(this->album=="") this->album = "未知专辑";

    //设置uuid
    uuid = QUuid::createUuid().toString();
}

void Music::setIsLike(const bool like)
{
    this->isLike = like;
}

void Music::setIsHistory(const bool history)
{
    this->isHistory = history;
}

void Music::setUuid(const QString &uuid)
{
    this->uuid = uuid;
}

void Music::setName(const QString &name)
{
    this->name = name;
}

void Music::setSinger(const QString &singer)
{
    this->singer = singer;
}

void Music::setAlbum(const QString &album)
{
    this->album = album;
}

void Music::setSongTime(const qint64 songtime)
{
    this->songtime = songtime;
}

void Music::setUrl(const QString &url)
{
    this->url = url;
}

QString Music::getUuid()
{
    return this->uuid;
}

QString Music::getName()
{
    return this->name;
}

QString Music::getSinger()
{
    return this->singer;
}

QString Music::getAlbum()
{
    return this->album;
}

qint64 Music::getSongTime()
{
    return this->songtime;
}

bool Music::getIsLike()
{
    return this->isLike;
}

bool Music::getIsHistory()
{
    return this->isHistory;
}

QUrl Music::getUrl()
{
    return this->url;
}

QString Music::getLrcFilePath()
{
    QString lrcPath = url.toLocalFile();
    int i = lrcPath.size()-1;
    while(i>=0 && lrcPath[i]!='.') --i;
    return lrcPath.left(i+1) + "lrc";
}

void Music::insertMusicToDB()
{
    QSqlQuery query;
    //1. 查询歌曲是否已经存入数据库中了
    query.prepare("SELECT EXISTS (SELECT 1 FROM QQMusicInfo WHERE musicId = ?)");
    query.addBindValue(uuid);
    if(!query.exec()){
        qDebug() << "查询失败: " << query.lastError().text();
        return;
    }
    if(query.next()){
        bool isExists = query.value(0).toBool();

        //存在更新即可
        if(isExists)
        {
            query.prepare("UPDATE QQMusicInfo SET isLike=?, isHistory=? WHERE musicId = ?");
            query.addBindValue(isLike ? 1 : 0);
            query.addBindValue(isHistory ? 1 : 0);
            query.addBindValue(uuid);
            if(!query.exec()){
                qDebug() << "更新失败: " << query.lastError().text();
                return;
            }
        }
        //不存在 , 插入
        else
        {
            query.prepare("INSERT INTO QQMusicInfo (musicId, musicName, musicSinger, musicAlbum,\
                            musicUrl, duration, isLike, isHistory) VALUES(?,?,?,?,?,?,?,?)");
            query.addBindValue(uuid);
            query.addBindValue(name);
            query.addBindValue(singer);
            query.addBindValue(album);
            query.addBindValue(url.toString());
            query.addBindValue(songtime);
            query.addBindValue(isLike ? 1 : 0);
            query.addBindValue(isHistory ? 1 : 0);
            if(!query.exec()){
                qDebug() << "插入失败: " << query.lastError().text();
                return;
            }
        }
    }
}

